package com.bridgeintelligent.tag.engine.mvc.service.impl;

import com.bridgeintelligent.tag.engine.api.EngineException;
import com.bridgeintelligent.tag.engine.api.rsp.DimLastMonthRsp;
import com.bridgeintelligent.tag.engine.api.rsp.DimMonitorDetailRsp;
import com.bridgeintelligent.tag.engine.api.rsp.DimTop10Rsp;
import com.bridgeintelligent.tag.engine.api.rsp.Page;
import com.bridgeintelligent.tag.engine.es.model.DimMonitorDetailModel;
import com.bridgeintelligent.tag.engine.es.model.LastMonthModel;
import com.bridgeintelligent.tag.engine.es.model.Top10Model;
import com.bridgeintelligent.tag.engine.mvc.service.IDimMonitorDetailService;
import com.bridgeintelligent.tag.engine.mvc.service.IDimMonitorService;
import com.bridgeintelligent.tag.engine.mvc.service.IDimTop10Service;
import com.bridgeintelligent.tag.engine.mvc.service.ILastMonthService;
import com.bridgeintelligent.tag.mapper.DimDataMapper;
import com.wayneleo.quickstart.framework.ExceptionCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;

/**
 * @description
 * @author: grp 2020-10-12 10:29
 **/
@Service
@AllArgsConstructor
@Slf4j
public class DimMonitorServiceImpl implements IDimMonitorService {
    private final ILastMonthService lastMonthService;
    private final IDimTop10Service top10Service;
    private final IDimMonitorDetailService detailService;
    private final DimDataMapper dimDataMapper;
    @Override
    public DimLastMonthRsp lastMonth(LastMonthModel model) {
        try{
            // 获取上个月日期
            String date = getLastMonthString();
            // 离散
            if(StringUtils.equals("1", model.getGroupType())){
                return lastMonthService.discrete(model.getDimId(), date);
            }
            // 默认分组
            if(StringUtils.equals("2", model.getGroupType())){
                return lastMonthService.defaultGroup(model.getDimId(), date, model.getType());
            }
            // 自定义分组
            if(StringUtils.equals("3", model.getGroupType())){
                return lastMonthService.customize(model.getDimId(), date, model.getItems(), model.getType());
            }
        }catch (Exception e){
            log.error("last month es查询出错", e);
            return DimLastMonthRsp.of().setData(DimLastMonthRsp.R.of());
        }
        throw new EngineException(new ExceptionCode(999,"不支持的统计类型"));
    }

    /**
     * 获取上个月时间
     * @return 日期
     */
    private String getLastMonthString() throws ParseException {
        String lastMonth = dimDataMapper.findLastMonth();
        return DateFormatUtils.format(DateUtils.parseDate(lastMonth, "yyyyMM"), "yyyy-MM");
    }

    @Override
    public DimTop10Rsp top10(Top10Model model) {
        try{
            // 离散
            if(StringUtils.equals("1", model.getGroupType())){
                return top10Service.discrete(model);
            }
            // 默认分组
            if(StringUtils.equals("2", model.getGroupType())){
                return top10Service.defaultGroup(model);
            }
            // 自定义分组
            if(StringUtils.equals("3", model.getGroupType())){
                return top10Service.customize(model);
            }
        }catch (Exception e){
            log.error("dim monitor top10 es查询出错", e);
            return DimTop10Rsp.of().setData(DimTop10Rsp.R.of());
        }
        throw new EngineException(new ExceptionCode(999,"不支持的统计类型"));
    }

    @Override
    public DimMonitorDetailRsp detail(DimMonitorDetailModel model) {
        try{
            // 离散
            if(StringUtils.equals("1", model.getGroupType())){
                return detailService.discrete(model);
            }
            // 默认分组
            if(StringUtils.equals("2", model.getGroupType())){
                return detailService.defaultGroup(model);
            }
            // 自定义分组
            if(StringUtils.equals("3", model.getGroupType())){
                return detailService.customize(model);
            }
        }catch (Exception e){
            log.error("dim monitor detail es查询出错", e);
            return DimMonitorDetailRsp.of().setData(Page.of());
        }
        throw new EngineException(new ExceptionCode(999,"不支持的统计类型"));
    }
}
